package com.atguigu.suanfa;

import java.util.HashMap;
import java.util.Map;
/**
 * @Author: lijunyao
 * LeetCode第一题：两数之和
 * hash   o(n)
 */
public class Solution2 {
    public int[] twoSum(int[] nums, int target) {
    Map<Integer,Integer> map = new HashMap<>(nums.length);
        for (int i = 0; i < nums.length; i++) {
            int partnerNumber = target - nums[i];
            if (map.containsKey(partnerNumber)) {
                return new int[]{map.get(partnerNumber),i};
            }
            map.put(nums[i],i);
        }
        return null;
    }
}
